from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect,HttpResponse
from db import models
white_list = ['/user/login/','/user/index','/static/']
class logincheck(MiddlewareMixin):
    def process_request(self,request):
        path_info =  request.path_info  #获取请求地址
        user_name = request.session.get("user_name") #获取session用户名
        if user_name:
            return None
        elif path_info in white_list:
            return  None
        else:
            return HttpResponse("请联系管理员")

        # if path_info in white_list: #判断请求地址是否白名单
        #     return None
        # else:
        #     user_url = models.userinfo.objects.filter(user_name=user_name).values("user_url__url_path")
        #     if user_name == "admin":
        #         return None
        #     elif path_info in user_url:   #判断请求地址是否授权用户
        #         return  None
        #     else:
        #         return redirect("/user/login")
